package com.ltg.urban.domain.project.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltg.framework.util.http.PageInfo;
import com.ltg.framework.util.http.Result;
import com.ltg.urban.domain.file.service.FileInfoService;
import com.ltg.urban.domain.project.data.request.ApplyCapitalReq;
import com.ltg.urban.domain.project.data.request.ApproveCapitalReq;
import com.ltg.urban.domain.project.data.response.CapitalDetailPageResp;
import com.ltg.urban.domain.project.entity.UrbanProject;
import com.ltg.urban.domain.project.entity.UrbanProjectCapital;
import com.ltg.urban.domain.project.entity.UrbanProjectCapitalDetail;
import com.ltg.urban.domain.project.entity.UrbanProjectRecord;
import com.ltg.urban.domain.project.mapper.UrbanProjectCapitalDetailMapper;
import com.ltg.urban.domain.project.mapper.UrbanProjectCapitalMapper;
import com.ltg.urban.domain.project.service.UrbanProjectCapitalDetailService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p> ClassName: UrbanProjectCapitalDetailServiceImpl </p>
 * <p> Package: com.ltg.urban.domain.project.service.impl </p>
 * <p> Description: </p>
 * <p></p>
 *
 * @Author: LTG
 * @Create: 2023/3/1 - 11:07
 * @Version: v1.0
 */

@Service
@RequiredArgsConstructor
public class UrbanProjectCapitalDetailServiceImpl extends ServiceImpl<UrbanProjectCapitalDetailMapper, UrbanProjectCapitalDetail> implements UrbanProjectCapitalDetailService {

    private final UrbanProjectCapitalDetailMapper urbanProjectCapitalDetailMapper;

    private final FileInfoService fileInfoService;

    @Override
    public PageInfo<CapitalDetailPageResp> pageList(Page<CapitalDetailPageResp> page) {
        Page<CapitalDetailPageResp> page1 = urbanProjectCapitalDetailMapper.page(page);
        for (CapitalDetailPageResp details : page1.getRecords()) {
            details.setFileUrl(fileInfoService.getFileUrl(details.getFileId()));
        }
        return new PageInfo<>(page1);
    }

    @Override
    public Result<UrbanProjectCapitalDetail> employeeApplyCapital(Long capitalId, ApplyCapitalReq req) {
        UrbanProjectCapitalDetail projectCapitalDetail = UrbanProjectCapitalDetail.builder()
                .capitalId(capitalId)
                .fee(req.getFee())
                .applyUser(req.getApplyUser())
                .approveResult(0)
                .applyUse(req.getApplyUse())
                .fileId(req.getFileId())
                .status(0)
                .build();
        urbanProjectCapitalDetailMapper.insert(projectCapitalDetail);
        return Result.success(projectCapitalDetail);
    }

    private final UrbanProjectCapitalMapper urbanProjectCapitalMapper;

    @Override
    public Result<UrbanProjectCapitalDetail> approveCapital(Long detailId, ApproveCapitalReq req) {
        UrbanProjectCapitalDetail projectCapitalDetail = urbanProjectCapitalDetailMapper.selectById(detailId);
        projectCapitalDetail.setApprover(req.getApprover());
        projectCapitalDetail.setStatus(1);
        if (req.getApproveResult().equals(1)) {
            UrbanProjectCapital urbanProjectCapital = urbanProjectCapitalMapper.selectById(projectCapitalDetail.getCapitalId());
            BigDecimal sum = urbanProjectCapital.getActualCost().add(projectCapitalDetail.getFee());
            urbanProjectCapital.setActualCost(sum);
            projectCapitalDetail.setApproveResult(req.getApproveResult());
            projectCapitalDetail.setApproveTime(LocalDateTime.now());
            projectCapitalDetail.setApproveDescription(req.getApproveDescription());
            urbanProjectCapitalMapper.updateById(urbanProjectCapital);
        } else {
            projectCapitalDetail.setApproveResult(req.getApproveResult());
            projectCapitalDetail.setApproveDescription(req.getApproveDescription());
        }
        urbanProjectCapitalDetailMapper.updateById(projectCapitalDetail);
        return Result.success(projectCapitalDetail);
    }

    @Override
    public Result<UrbanProjectCapitalDetail> anewApply(Long detailId, ApplyCapitalReq req) {
        UrbanProjectCapitalDetail projectCapitalDetail = urbanProjectCapitalDetailMapper.selectById(detailId);
        projectCapitalDetail.setApplyUser(req.getApplyUser());
        projectCapitalDetail.setApplyUse(req.getApplyUse());
        projectCapitalDetail.setFee(req.getFee());
        projectCapitalDetail.setFileId(req.getFileId());
        projectCapitalDetail.setApproveResult(0);
        projectCapitalDetail.setStatus(0);
        urbanProjectCapitalDetailMapper.updateById(projectCapitalDetail);
        return Result.success(projectCapitalDetail);
    }
}
